package io.kiki.sba.registry.server.data_store.remoting.metaserver.handler;

import io.kiki.sba.registry.api.Channel;
import io.kiki.sba.registry.common.model.Node.NodeType;
import io.kiki.sba.registry.common.model.Response;
import io.kiki.sba.registry.common.model.metaserver.RemoteDatumClearEvent;
import io.kiki.sba.registry.common.model.metaserver.RemoteDatumClearEvent.DatumType;
import io.kiki.sba.registry.server.data_store.multi.cluster.storage.MultiClusterDatumService;
import io.kiki.sba.registry.server.shared.remoting.AbstractClientHandler;
import io.kiki.sba.registry.util.StringUtil;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;


public class RemoteDatumClearEventHandler extends AbstractClientHandler<RemoteDatumClearEvent> {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private MultiClusterDatumService multiClusterDatumService;


    @Override
    public Class interest() {
        return RemoteDatumClearEvent.class;
    }

    @Override
    protected NodeType getConnectNodeType() {
        return NodeType.central_control;
    }

    @Override
    public void checkParam(@NonNull RemoteDatumClearEvent request) {

        StringUtil.checkNotBlank(request.getRemoteDataCenter(), "RemoteDataCenter");
        StringUtil.nullValue(request.getDatumType(), "DatumType");
        if (request.getDatumType() == DatumType.group) {
            StringUtil.checkNotBlank(request.getGroup(), "Group");
        } else {
            StringUtil.checkNotBlank(request.getDataInfoId(), "DataInfoId");
        }
    }


    @Override
    public Object doHandle(Channel channel, RemoteDatumClearEvent request) {
        multiClusterDatumService.clear(request);
        return Response.buildSuccessResponse();
    }

    @Override
    public Response buildFailedResponse(String msg) {
        return Response.buildFailedResponse(msg);
    }

    @Override
    public Executor getExecutor() {
        return threadPoolExecutor;
    }
}
